tags: Easy、Linked List
Given the head of a singly linked list, return the middle node of the linked list.
If there are two middle nodes, return the second middle node.
將原本的fast->next->next;
改成使用fast->next;
寫兩次,有些編譯器可以更好的去改善效能跟速度,所以這樣寫的速度會更快
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* middleNode(struct ListNode* head) {
struct ListNode *slow = head;
struct ListNode *fast = head;
while (fast != NULL && fast->next != NULL) {
slow = slow->next;
fast = fast->next;
fast = fast->next;
}
return slow;
}